const Router = require("koa-router");
const jsonwebtoken = require("jsonwebtoken");
const db = require("../../sql/mysql");
let router = new Router();
const accountNumber = {
  userName: "张三",
  password: 123456,
  id: 100,
};
const SECRET = "laotie666"; //随意输入
router.post("/login", async (ctx) => {
  let { userName, password } = ctx.request.body;
  let result = {};
  if (
    userName == accountNumber.userName &&
    password == accountNumber.password
  ) {
    result = {
      code: 200,
      message: "登录成功",
      token: jsonwebtoken.sign(
        { userName, id: accountNumber.id }, // 加密userToken
        SECRET,
        { expiresIn: "30s" }
      ),
    };
  } else {
    result = {
      code: 100,
      message: "登录失败,用户名或密码错误",
    };
  }

  ctx.body = {
    result,
  };
});

module.exports = router;
